const request = require('request')
var config = require('../appConfig').qqconfig;
var getQqOpenid = require('./getQqOpenid');
/*
* https://graph.qq.com/user/get_user_info?access_token=YOUR_ACCESS_TOKEN&
* oauth_consumer_key=YOUR_APP_ID&openid=YOUR_OPENID
*/
var getUserInfo_qq = function(code) {
    return new Promise(function (resolve, reject) {
        getQqOpenid(code).then(function (response) {
            var func = response['func'];  //callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
            var access_token = response['access_token'];
            var data = JSON.parse(func.split('(')[1].split(')')[0]);
            var url = config.qqInfoUrl+'?access_token='+access_token+
                '&oauth_consumer_key='+config.appid + '&openid='+data.openid
            request.get(url, (err, res, body) => {
                var body = JSON.parse(body);
                if (err) {
                    reject(err)
                }
                if (body.errcode) {
                    reject(body.errcode + body.errmsg)
                }
                var tempObj = {};
                tempObj['openid'] = data.openid ;
                tempObj['nickname'] = body.nickname ;
                tempObj['head_img'] = body.figureurl_qq_2 ;
                resolve(tempObj)
            })
        })
    })

}
// 只要向外暴露一个获取值的方法就可以了
module.exports = getUserInfo_qq
